package com.shinewind.info.controller;

import cn.hutool.core.util.StrUtil;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.common.util.FastDFSClientUtil;
import com.shinewind.info.service.IModelInfoService;
import com.shinewind.info.service.IUserDetailService;
import com.shinewind.pojo.entity.ModelInfo;
import com.shinewind.pojo.entity.UserDetail;
import com.shinewind.pojo.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/userInfo")
@Api(value = "用户头像上传接口",description = "用于用户头像上传")
public class UserUploadController {
    @Autowired(required = false)
    private FastDFSClientUtil fastDFSClientUtil;
    @Value("${fileServerUrl}")
    private String fileServerUrl;
    @Autowired
    private IUserDetailService iUserDetailService;
    @Autowired
    private IModelInfoService iModelInfoService;

    @PostMapping("/upload/{uId}")
    @ApiOperation(value = "用户头像上传",notes = "用户头像上传",response = Result.class)
    public Result uploadUserAvatar(@PathVariable("uId") String uId,MultipartFile file) throws IOException {
        System.out.println("fileName:"+file.getOriginalFilename());
        System.out.println("size:"+file.getSize());
        System.out.println("stream:"+file.getInputStream());
        System.out.println("fileServerUrl:"+fileServerUrl);
        if(file == null){
            throw new RuntimeException("文件不存在");
        }
        else{
            //上传头像
            String fileId = fastDFSClientUtil.uploadFile(file);
            System.out.println("fileId:"+fileId);
            String fileUrl = fileServerUrl + fileId;
            if(StrUtil.isNotBlank(fileUrl)){
                //将用户的头像地址更改
                Boolean flag = iUserDetailService.lambdaUpdate().eq(UserDetail::getUId,uId)
                        .set(UserDetail::getAvatar,fileUrl).update();
                if(flag){
                    return Result.success(true,messageConstant.UPLOAD_SUCCESS);
                }
                else{
                    return Result.fail(false, messageConstant.UPDATE_ERROR);
                }
            }
        }
        return Result.fail(false, messageConstant.UPDATE_ERROR);
    }

    //后台接口,上传关联视频
    /*@PostMapping("/upload/video/{mId}")
    @ApiOperation(value = "模型关联视频上传",notes = "模型关联视频上传",response = Result.class)
    public Result uploadRelateVideo(@PathVariable("mId") String mId,MultipartFile file) throws IOException {
        System.out.println("fileName:"+file.getOriginalFilename());
        System.out.println("size:"+file.getSize());
        System.out.println("stream:"+file.getInputStream());
        System.out.println("fileServerUrl:"+fileServerUrl);
        if(file == null){
            throw new RuntimeException("文件不存在");
        }
        else{
            //上传关联视频
            String fileId = fastDFSClientUtil.uploadFile(file);
            System.out.println("fileId:"+fileId);
            String fileUrl = fileServerUrl + fileId;
            if(StrUtil.isNotBlank(fileUrl)){
                //将模型关联视频更改
                Boolean flag = iModelInfoService.lambdaUpdate().eq(ModelInfo::getMId,mId)
                        .set(ModelInfo::getVideoHref,fileUrl).update();
                if(flag){
                    return Result.success(true,messageConstant.UPLOAD_SUCCESS);
                }
                else{
                    return Result.fail(false, messageConstant.UPDATE_ERROR);
                }
            }
        }
        return Result.fail(false, messageConstant.UPDATE_ERROR);
    }*/
}
